Using Git and Github
April 15, 2025
Compiled software are a bunch of files. At each software improvment, developers want the software to be stable with no regression. For that, they have to know what files have changed between snapshots.
add & commitadd & commitadd & commitadd & commitadd & commitadd & commitbranchesbranchesmain par défault): c’est un série de commentaires (commit)commit) est ce que l’on appelle la tête de la branche (HEAD), elle contient la version la plus à jour des fichiers.commit) est attachée une version des fichiers.local vs remotegit init)git add)git commit)git fetch & git push)git fetch & git pull)git —versiongit configFor more details: https://happygitwithr.com/install-git
In order to reduce the learning curve, we will learn the R Studio way of interacting with git
So many other ways to interact with git:
gert packageAll the key concepts and process still the same!
To use git in Rstudio, you need to create a new Project. Let’s start by creating our project directory.
Open RStudio, in the top right corner create a new project
You can now look under the git tab, you should see this:
Create a new file: go to the menu File => New file => R Script. Make sure to save it with the name script_1.R.
What happens in the git tab?
This is as simple as “checking off” the file in the Git tab. This is called Staging the file.
What is a gitignore? A gitignore is a file that lists the file you never want git to track. It can match certain file names (for instance, .csv or .tif files). This can be useful in case you need to make sure certain files (like data files or large files), do not get added.
Click on commit on the top of the file list. This window should appear:
Before committing anything you need to add a commit message. It is important to add a useful message to your commit, a bit like a journal entry, so that you can remember what you committed.
Click on commit in order to commit the changes!
Let’s make sure you have the following packages installed on your computer:
usethis to create a GitHub tokengitcreds to store the token in your local systemIn the first workshop, we learned the basics of Git. We covered:
.gitignore file (relevant for sensitive data)git Workflow Overview ReminderThe typical workflow:
Tip
We can make an analogy with taking a family picture, where each family member would represent a file.
Staging files is like deciding which family member(s) are going to be on your next picture Committing is like taking the picture
Step 1. For those who don’t have an account, sign up on GitHub using this link
We have to store a token in our local system. This token is used to authenticate your computer with Github.
Tip
Token = password
Run the following command in RStudio
ghp_Run the following command in RStudio
This command will open a prompt in RStudio
Paste the token you copied in the previous step
Assess if everything is fine and the credentials are OK
Note
This way, you will not have to enter your credentials every time you push or pull from GitHub. If you want to know more on this authentification process, have a look at the usethis documentation
Note
usethis::use_github(private = TRUE) if you want your repository to be private, because you are working with sensitive data
The configuration is done and the local is now sync! Lets have a quick look at the Github repo on the website.
Lets go back on RStudio and check the Git tab. Two new buttons Pull and Push are now available.
Commit, move a changed local file to your local staging areaPull, will get the latest remote version and try to merge it with your local versionPush, will send your local version to the remote version of the repository (in our case GitHub)You have to repeat step 2-4, every time you edit, add, delete a new file if you want to keep your local and remote repository in sync.
If I’m a collaborator on a project, I need to be able to get the latest version of the code from the remote repository.
To accomplish this, I Clone the remote repository to my local computer. Clone means to copy the entire contents of a GitHub repository to your local computer.
Navigate to the repository on GitHub and click on Code. Select HTTPS and copy the link.
Clone, copy the entire contents of a GitHub repository to your local computer (done once per computer)Commit, move a changed local file to your local staging areaPull, get file(s) from the remote to your local computer – opposite of a “push”Push, move file(s) to the remote from your local computer – opposite of a “pull”20:00
Tip
Reminder on git file status:
Invite a collaborator to your repository on GitHub.
git conflictsgit conflicts on RStudiogit conflicts on RStudiogit conflictsIn teams of 2, edit the same file at the same time in the same repository. Then, try to push your changes to the remote repository. A conflict should occur, let’s try to resolve it.
15:00
A lot of the content of this workshop come from or are based on the following resources: